package com.example.carrierapp.base;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.example.lb.fingerdemo.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

public abstract class BaseActivity extends AppCompatActivity {
    protected static Integer sMiuiVersionCode;
    private TextView mTvTitle;
    protected ImageButton mIbBack;
    protected View mBottomLine;
    protected boolean mOnOverrideBackClick = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (sMiuiVersionCode == null) {
            sMiuiVersionCode = getMIUIVersionCode();
        }
        if (sMiuiVersionCode >= 4 && enableMIUIStatusBarCompat()) { //miui v6
            if (Build.VERSION.SDK_INT >= 21) {
                setMiuiStatusBarDarkMode(true);
                getWindow().setStatusBarColor(ActivityCompat.getColor(this, R.color.white));
            }
        }
        int contentRes = getContentView();
        if (contentRes > 0) {
            setContentView(contentRes);
        }
        initDatabinding();
        mTvTitle = findViewById(R.id.tv_title);
        if (!TextUtils.isEmpty(getTitleString()) && mTvTitle != null) {
            mTvTitle.setText(getTitleString());
        }
        mIbBack = findViewById(R.id.ib_title_left);
        if (mIbBack != null) {
            if (!hideBackView()) {
                if (!onOverrideBackClick()) {
                    mIbBack.setOnClickListener(v -> finish());
                }
            } else {
                mIbBack.setVisibility(View.GONE);
            }
        }
        mBottomLine = findViewById(R.id.tv_title_line);
        if (mBottomLine != null) {
            mBottomLine.setVisibility(showBottomLine() ? View.VISIBLE : View.GONE);
        }
        initData();
        initView();
        initEvent();
    }

    protected boolean showBottomLine() {
        return true;
    }

    protected void setTitleString(String msg) {
        if (mTvTitle != null) {
            mTvTitle.setText(msg);
        }
    }

    protected void hideBackIcon() {
        if (mIbBack != null) {
            mIbBack.setVisibility(View.GONE);
        }
    }

    public void initView() {

    }

    public void initData() {

    }

    public void initEvent() {

    }

    public void initDatabinding() {

    }

    protected boolean onOverrideBackClick() {
        return mOnOverrideBackClick;
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public abstract int getContentView();

    public abstract String getTitleString();

    private int getMIUIVersionCode() {
        Properties p = new Properties();
        try (FileInputStream fis = new FileInputStream(Environment.getRootDirectory() + "/build.prop")) {
            p.load(fis);
            final String code = p.getProperty("ro.miui.ui.version.waybillCode");
            return !TextUtils.isEmpty(code) && TextUtils.isDigitsOnly(code) ? Integer.parseInt(code) : -1;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    protected boolean enableMIUIStatusBarCompat() {
        return true;
    }

    // only for miui
    private Integer darkModeFlag;
    private Method setExtraFlags;
    private boolean callExtraFlags;

    protected boolean setMiuiStatusBarDarkMode(boolean darkmode) {
        if (callExtraFlags && setExtraFlags == null) {
            return false;
        }
        try {
            if (darkModeFlag == null) {
                @SuppressLint("PrivateApi")
                Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                this.darkModeFlag = field.getInt(layoutParams);
            }
            if (!callExtraFlags) {
                callExtraFlags = true;
                Class<? extends Window> clazz = getWindow().getClass();
                this.setExtraFlags = clazz.getMethod("setExtraFlags", int.class, int.class);
            }
            setExtraFlags.invoke(getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            if (darkModeFlag == null) {
                darkModeFlag = 0;
            }
        }
        return false;
    }

    protected boolean hideBackView() {
        return false;
    }

    public void back(View view) {
        finish();
    }
}
